//
//  KhxqViewController.m
//  zhihuiguanjia
//
//  Created by apple on 16/4/3.
//  Copyright © 2016年 MyCompany. All rights reserved.
//

#import "KhxqViewController.h"
#import "MainViewController.h"
#import "ZxViewController.h"  
#import "RzViewController.h"
#import "ZlViewController.h"
#import "FswViewController.h"
#import "CqViewController.h"
#import "UIImageView+WebCache.h"

static CGFloat const titleH = 30;
static CGFloat const navBarH = 64;
static CGFloat const maxTitleScale = 1.1;

#define YCKScreenW [UIScreen mainScreen].bounds.size.width
#define YCKScreenH [UIScreen mainScreen].bounds.size.height

@interface KhxqViewController ()<UIActionSheetDelegate>

@property (nonatomic, strong) UIButton *back;
@property (nonatomic, strong) NetworkManager *networkManager;
@property (nonatomic, strong) MBProgressHUD *HUD;

@property (nonatomic, weak) UIScrollView *titleScrollView;
@property (nonatomic, weak) UIScrollView *contentScrollView;
// 选中按钮
@property (nonatomic, weak) UIButton *selTitleButton;

@property (nonatomic, strong) NSMutableArray *buttons;

@end
@implementation KhxqViewController
- (void)viewDidLoad
{
    [self.navigationController setNavigationBarHidden:YES];
    self.networkManager = [[NetworkManager alloc] init];
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    self.HUD.delegate = self;
    [super viewDidLoad];
    
}

- (IBAction)backToPre:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}



- (NSMutableArray *)buttons
{
    if (!_buttons)
    {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self getZhuhuxq];
    [self getWmpage];
}

-(void) getWmpage{
    [self setupTitleScrollView];
    [self setupContentScrollView];
    [self addChildViewController];
    [self setupTitle];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.contentScrollView.contentSize = CGSizeMake(self.childViewControllers.count * YCKScreenW, 0);
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.delegate = self;
}

#pragma mark - 设置头部标题栏
- (void)setupTitleScrollView
{
    // 判断是否存在导航控制器来判断y值
    CGFloat y = self.navigationController ? navBarH+130 : 0;
    CGRect rect = CGRectMake(0, y, YCKScreenW, titleH);
    
    UIScrollView *titleScrollView = [[UIScrollView alloc] initWithFrame:rect];
    titleScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:titleScrollView];
    self.titleScrollView = titleScrollView;
}

#pragma mark - 设置内容
- (void)setupContentScrollView
{
    CGFloat y = CGRectGetMaxY(self.titleScrollView.frame);
    CGRect rect = CGRectMake(0, y, YCKScreenW, YCKScreenH - navBarH);
    
    UIScrollView *contentScrollView = [[UIScrollView alloc] initWithFrame:rect];
    //    contentScrollView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:contentScrollView];
    
    self.contentScrollView = contentScrollView;
}

#pragma mark - 添加子控制器
- (void)addChildViewController
{
    CqViewController *vc1 = (CqViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"CqViewController"];
    vc1.zhuhuId = _zhuhuId;
    vc1.title = @"产权";
    [self addChildViewController:vc1];
    
    ZlViewController *vc2 = (ZlViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"ZlViewController"];
    vc2.zhuhuId = _zhuhuId;
    vc2.title = @"租赁";
    [self addChildViewController:vc2];
    
    FswViewController *vc3 = (FswViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"FswViewController"];
    vc3.zhuhuId = _zhuhuId;
    vc3.title = @"附属物";
    [self addChildViewController:vc3];
    
    ZxViewController *vc4 = (ZxViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"ZxViewController"];
    vc4.zhuhuId = _zhuhuId;
    vc4.title = @"装修";
    [self addChildViewController:vc4];
    
    RzViewController *vc5 = (RzViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"RzViewController"];
    vc5.zhuhuId = _zhuhuId;
    vc5.title = @"入住";
    [self addChildViewController:vc5];
    
}

#pragma mark - 设置标题
- (void)setupTitle
{
    NSUInteger count = self.childViewControllers.count;
    
    CGFloat x = 0;
    CGFloat w = 63;
    CGFloat h = titleH;
    
    for (int i = 0; i < count; i++)
    {
        UIViewController *vc = self.childViewControllers[i];
        
        x = i * w;
        CGRect rect = CGRectMake(x, 0, w, h);
        UIButton *btn = [[UIButton alloc] initWithFrame:rect];
        
        btn.tag = i;
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        
        [btn addTarget:self action:@selector(chick:) forControlEvents:UIControlEventTouchDown];
        
        [self.buttons addObject:btn];
        [self.titleScrollView addSubview:btn];
        
        if (i == 0)
        {
            [self chick:btn];
        }
        
    }
    self.titleScrollView.contentSize = CGSizeMake(count * w, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
}

// 按钮点击
- (void)chick:(UIButton *)btn
{
    [self selTitleBtn:btn];
    
    NSUInteger i = btn.tag;
    CGFloat x = i * YCKScreenW;
    
    [self setUpOneChildViewController:i];
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
    
}
// 选中按钮
- (void)selTitleBtn:(UIButton *)btn
{
    [self.selTitleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.selTitleButton.transform = CGAffineTransformIdentity;
    
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn.transform = CGAffineTransformMakeScale(maxTitleScale, maxTitleScale);
    
    self.selTitleButton = btn;
    [self setupTitleCenter:btn];
}

- (void)setUpOneChildViewController:(NSUInteger)i
{
    CGFloat x = i * YCKScreenW;
    
    UIViewController *vc = self.childViewControllers[i];
    
    if (vc.view.superview) {
        return;
    }
    vc.view.frame = CGRectMake(x, 0, YCKScreenW, YCKScreenH - self.contentScrollView.frame.origin.y);
    
    [self.contentScrollView addSubview:vc.view];
    
}

- (void)setupTitleCenter:(UIButton *)btn
{
    CGFloat offset = btn.center.x - YCKScreenW * 0.5;
    
    if (offset < 0)
    {
        offset = 0;
    }
    
    CGFloat maxOffset = self.titleScrollView.contentSize.width - YCKScreenW;
    if (offset > maxOffset)
    {
        offset = maxOffset;
    }
    
    [self.titleScrollView setContentOffset:CGPointMake(offset, 0) animated:YES];
    
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSUInteger i = self.contentScrollView.contentOffset.x / YCKScreenW;
    [self selTitleBtn:self.buttons[i]];
    [self setUpOneChildViewController:i];
}

// 只要滚动UIScrollView就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger leftIndex = offsetX / YCKScreenW;
    NSInteger rightIndex = leftIndex + 1;
    
    //    NSLog(@"%zd,%zd",leftIndex,rightIndex);
    
    UIButton *leftButton = self.buttons[leftIndex];
    
    UIButton *rightButton = nil;
    if (rightIndex < self.buttons.count) {
        rightButton = self.buttons[rightIndex];
    }
    
    CGFloat scaleR = offsetX / YCKScreenW - leftIndex;
    
    CGFloat scaleL = 1 - scaleR;
    
    
    CGFloat transScale = maxTitleScale - 1;
    leftButton.transform = CGAffineTransformMakeScale(scaleL * transScale + 1, scaleL * transScale + 1);
    
    rightButton.transform = CGAffineTransformMakeScale(scaleR * transScale + 1, scaleR * transScale + 1);
    
    
    UIColor *rightColor = [UIColor colorWithRed:scaleR green:0 blue:0 alpha:1];
    UIColor *leftColor = [UIColor colorWithRed:scaleL green:0 blue:0 alpha:1];
    
    [leftButton setTitleColor:leftColor forState:UIControlStateNormal];
    [rightButton setTitleColor:rightColor forState:UIControlStateNormal];
    
    
}

- (void)getZhuhuxq {
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                           _zhuhuId,@"zhuhu_id", nil];
    [self.networkManager requestWithMethod:zhuhuDetail parameter:param delegate:self];
    [self.HUD show:YES];
}


- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFinishedWithResult:(id)data {
    [self.HUD hide:YES];
    
    if (methodId == zhuhuDetail) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * result = [data objectForKeyOrNil:@"result"];
            
            if([result isEqualToString:@"000"]){
                _fangjianText.text = [[[data objectForKeyOrNil:@"zhuhu"] objectForKeyOrNil:@"louName"] stringByAppendingString: [[data objectForKeyOrNil:@"zhuhu"] objectForKeyOrNil:@"menpai"]];
               
                _nickNameText.text = [[[data objectForKeyOrNil:@"zhuhu"] objectForKeyOrNil:@"louName"] stringByAppendingString: [[data objectForKeyOrNil:@"zhuhu"] objectForKeyOrNil:@"nickName"]];
                
                _scmjText.text = [[[data objectForKeyOrNil:@"zhuhu"] objectForKeyOrNil:@"shiyongMianji"] stringValue];
                _ycmjText.text = [[[data objectForKeyOrNil:@"zhuhu"] objectForKeyOrNil:@"mianji"] stringValue];
                if([[data objectForKeyOrNil:@"zhuhu"] objectForKeyOrNil:@"louType"] == 0){
                    _shuxingText.text = @"住宅";
                } else if([[data objectForKeyOrNil:@"zhuhu"] objectForKeyOrNil:@"louType"] == 0){
                    _shuxingText.text = @"商用";
                } else if([[data objectForKeyOrNil:@"zhuhu"] objectForKeyOrNil:@"louType"] == 0){
                    _shuxingText.text = @"住宅商用两用";
                }
                NSLog(@"@@@@@@@@%@",[[data objectForKeyOrNil:@"zhuhu"] objectForKeyOrNil:@"houseType"]);
                
                NSString *hxtImgStr  = [NSString stringWithFormat:@"%@%@",SERVER_PATH,[[data objectForKeyOrNil:@"zhuhu"] objectForKeyOrNil:@"houseType"]];
                NSURL *url1 = [NSURL URLWithString:hxtImgStr];
                NSLog(@"@@@@@@@@%@",hxtImgStr);
                [_hxtImg sd_setImageWithURL:url1];
                 
            } else if([result isEqualToString:@"001"]){
                NSString * resultinfo = [data objectForKeyOrNil:@"resultinfo"];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                                message:[resultinfo stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
                                                               delegate:nil
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:nil];
                [alert show];
                
            }
        }
        
    }
    
}

@end
